﻿
@{
    ViewBag.Title = "发货单管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
    var warehouses = ViewBag.Warehouses as List<IOA.MES.DataAccess.VModel.VWarehouse>;
}

@section Contents{
    <style>
        .layui-form-label {
            width: auto;
        }

        .layui-table-cell {
            height: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.DeliveryWarehouse}}：</label>
                        <div class="layui-input-inline">
                            <select id="selWarehouses" lay-filter="selWarehouses" lay-search="">
                                <option value=""></option>
                                @foreach (var item in warehouses)
                                {
                                    <option value="@item.WarehouseCode">@item.WarehouseName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.DeliveryNo}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtDeliveryOrderNo" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.OrderNo}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtOrderNo" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.CustomerInfo}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtCustomer" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">{{lang.Field.DeliveryDate}}：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtRangeDate" value="@(DateTime.Today.AddMonths(-3).ToString("yyyy-MM-dd")+" 至 "+DateTime.Today.ToString("yyyy-MM-dd"))" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="button">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <button class="layui-btn layui-btn-normal layuiadmin-btn-list" type="button" onclick="editOrder(0)">
                            <i class="layui-icon layui-icon-addition layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form', 'laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                laydate.render({
                    elem: '#txtRangeDate'
                    , range: '至'
                });
                var options = {
                    elem: '#table'
                    , url: '/Warehouse/QueryDeliveryOrders'
                    , method: "post"
                    , where: { warehouseCode: 0, deliveryOrderNo: "", orderNo: "", customer: "", startDate: "@DateTime.Today.AddMonths(-3).ToString("yyyy-MM-dd")", endDate: "@DateTime.Today.ToString("yyyy-MM-dd")" }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', width: 60, type: "numbers" }
                        , { field: 'DeliveryNo', minWidth: 100, title: '发货单号' }
                        , { field: 'OrderNo', minWidth: 100, title: '订单号' }
                        , { field: 'ContractNo', minWidth: 100, title: '合同编号' }
                        , {
                            title: '发货仓库', minWidth: 180, templet: function (d) {
                                return d.WarehouseCode + "-" + d.WarehouseName;
                            }
                        }
                        , { field: 'DeliveryDateStr',title: '发货日期', minWidth: 110 }
                        , {
                            title: '客户信息', minWidth: 150, templet: function (d) {
                                return d.CustomerCode + "-" + d.CustomerName;
                            }
                        }
                        , { field: "Count", minWidth: 70, title: '数量' }
                        , {
                            title: '操作', minWidth: 250, fixed: "right", templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-normal" onclick="editOrder(\'' + d.DeliveryNo + '\')">编辑</button>\
                                           <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="deliveryPacks(\'' + d.DeliveryNo + '\')">发货清单</button>';
                                if (d.Status == 0) {
                                    str = str + '<button class="layui-btn layui-btn-sm" onclick="submit(\'' + d.DeliveryNo + '\')">确认发货</button>';
                                }
                                return str;
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    options.where.warehouseCode = $("#selWarehouses").val();
                    options.where.deliveryOrderNo = $("#txtDeliveryOrderNo").val();
                    options.where.orderNo = $("#txtOrderNo").val();
                    options.where.customer = $("#txtCustomer").val();
                    var rangeTime = $("#txtRangeDate").val().trim() == '' ? new Array() : $("#txtRangeDate").val().trim().split(' 至 ');
                    options.where.startDate = rangeTime.length <= 0 ? null : rangeTime[0];
                    options.where.endDate = rangeTime.length <= 0 ? null : rangeTime[1];

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }
            });
        }

        function editOrder(deliveryNo) {
            layer.open({
                type: 2,
                shade: [0.1, '#000'],
                area: ['550px', '600px'],
                content: "/Plan/DeliveryEdit?deliveryNo=" + deliveryNo,
                end: function () {
                    $("#btnSearch").click();
                }
            });
        }

        function deliveryPacks(deliveryNo) {
            parent.layui.index.openTabsPage("/Plan/DeliveryDetail?deliveryNo=" + deliveryNo, "发货清单(" + deliveryNo + ")");
        }

        function submit(deliveryNo) {
            layer.confirm('确定要发货吗？确认后柜号将不可修改', { icon: 3, title: '提示' }, function (index) {
                var loadId = layer.load();
                $.ajax({
                    url: "/Plan/SubmitDelivery",
                    data: {
                        deliveryNo: deliveryNo
                    },
                    complete: function () {
                        layer.close(loadId);
                    },
                    success: function (res) {
                        if (res.ErrCode != 200) {
                            layer.msg(res.ErrMsg, { icon: 5 });
                            return;
                        }
                        layer.msg("提交成功！", { icon: 6, time: 1500 });
                        $("#btnSearch").click();
                    }
                });
                layer.close(index);
            });
        }
    </script>
}
